package cn.kgc.ssm.controller;

import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.service.UserService;
import org.apache.ibatis.executor.statement.PreparedStatementHandler;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserController {

    @Autowired
    private UserService userService;

    private Logger logger = Logger.getLogger(UserController.class);

    @RequestMapping("/query_user_all")
    public String queryUser(Model model){
        List<User> users = userService.selectUserAll();
        model.addAttribute("userList",users);
        logger.info(users);
        return "user_info";
    }

    @RequestMapping("/transfer")
    public String transfer(){
        User formUser = userService.selectUserById(2);
        User toUser = userService.selectUserById(1);
        int n = userService.transfer(formUser, toUser, 1000.00);
        return "success";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/user_info")
    public String userInfo(){
        return "user_info";
    }

    @RequestMapping("/do_login")
    public String doLogin(User user, Model model, HttpSession session){

        User loginUser = userService.loginUser(user);

        if(null != loginUser){
            model.addAttribute("user",loginUser);
//            session.setAttribute("user",loginUser);

            return "user_info";
        }else {
            model.addAttribute("msg","用户名或密码错");
            return "login";
        }

    }
}
